/**
 * 树转数组
 */
const treeToList = (
  tree = [],
  idValue = null,
  childrenField = 'children',
  idField = 'id',
  parentIdField = 'parentId'
) => {
  const list = []
  if (!childrenField) childrenField = 'children'
  for (let i = 0, j = tree.length; i < j; i++) {
    const d = tree[i]
    const id = d[idField]
    if (!list.some(l => l[idField] === id)) {
      list.push(d)
    }
    if (parentIdField) d[parentIdField] = idValue
    const children = d[childrenField]
    if (children && children.length > 0) {
      const items = treeToList(
        children,
        id,
        childrenField,
        idField,
        parentIdField
      )
      const values = items.values()
      for (const v of values) {
        if (!list.some(l => l[idField] === v[idField])) {
          list.push(v)
        }
      }
    }
  }
  return list
}

const getTreeInItem = (list, tid) => {
  const treelist = treeToList(JSON.parse(JSON.stringify(list)))
  const item = treelist.find(item => item.id === tid)
  return item
}

const findTreeItem = (list, tid) => {
  let obj = {}
  for (let i = 0; i < list.length; i++) {
    const item = list[i];
    if (Array.isArray(item.children) && item.children.length > 0) {
      obj = findTreeItem(item.children, tid)
    }
    if (tid === String(item.id)) {
      obj = item
      continue
    }
  }
  return obj
}

// 获取选中对应得树
const findTreeList = (list, tids) => {
  return tids.map(e => {
    return {
      ...getTreeInItem(list, e)
    }
  }).filter(e => !!e.id)
}

// 获取树得id
const fundTreeListIds = (treelist, tids) => {
  let treeList = findTreeList(treelist, tids);
  return treeList.reduce((prv, cur) => {
    if (Array.isArray(cur.children) && cur.children.length > 0) {
      return prv.concat(cur.children.reduce((cprv, child) => {
        return cprv.concat(child.id);
      }, []))
    } else {
      return prv.concat(cur.id);
    }
  }, [])
}

// 获取树得id
const fundTreeList = (tree, tids) => {
  let treeList = findTreeList(JSON.parse(JSON.stringify(tree)), tids);
  console.log('fund', treeList)
  return treeList.reduce((prv, cur) => {
    if (Array.isArray(cur.children) && cur.children.length > 0) {
      return prv.concat(cur.children.reduce((cprv, child) => {
        return cprv.concat({
          ...child
        });
      }, []))
    } else {
      return prv.concat({
        ...cur
      });
    }
  }, [])
}

const getAllIds = (node, ids = []) => {
  if (node && !Array.isArray(node.children)) {
    ids.push({
      ...node
    });
  }
  if (node && node.children) {
    node.children.forEach(child => getAllIds(child, ids));
  }
  return ids;
}

// 获取树得id
const getAllTreeItem = (tree, tids) => {
  let treeList = findTreeList(JSON.parse(JSON.stringify(tree)), tids);
  console.log('fund', treeList)
  return treeList.map(e => {
    return getAllIds(e)
  })
}

/**
 * 获取对应的 需要获取图片的，对应的树型id 
 * @param {*} constents 选择的内容ids
 * @param {*} tree 树型数据
 */
// export const getApplyContentsArrayByTree = (constents, ttree = []) => {
export const getApplyContentsArrayByTree = (constents, ttree = []) => {
  // 这个是 上面查阅内容的cid 对应的，treeids 是 树结构的id 数组
  // 你知道对应的是那几个, 直接映射到这里，我们就知道哪个对应哪个了
  let tree = JSON.parse(JSON.stringify(ttree))

  // 这个后端没有给我们对应的的树型数据id
  // 比如到线上了，树型数据多了，还有很多，需要手动写死的这个，就类似你刚写的 if ... else if ...
  let contentMap = [{
      cid: '01',
      treeIds: ['01']
    },
    {
      cid: '02',
      treeIds: ['02']
    },
    {
      cid: '03',
      treeIds: ['03']
    },
    {
      cid: '04',
      treeIds: ['0401']
    },
    {
      cid: '05',
      treeIds: ['0402']
    },
    {
      cid: '06',
      treeIds: ['0403']
    },
    {
      cid: '07',
      treeIds: ['0404']
    },
    {
      cid: '08',
      treeIds: ['05']
    },
    {
      cid: '09',
      treeIds: ['06']
    },
    {
      cid: '10',
      treeIds: ['07']
    },
    {
      cid: '11',
      treeIds: ['08']
    },
    {
      cid: '12',
      treeIds: ['0908']
    },
    {
      cid: '13',
      treeIds: ['0909']
    },
    {
      cid: '14',
      treeIds: ['0910']
    },
    {
      cid: '15',
      treeIds: ['0911']
    },
    {
      cid: '16',
      treeIds: ['10', "11"]
    },
    // ...
  ]

  // 得到需要树型的ids
  let selectTreeIds = contentMap.filter(f => constents.includes(f.cid))
    .reduce((prv, cur) => {
      prv.push(...cur.treeIds)
      return prv
    }, [])
  let treeItemList = getAllTreeItem(tree, selectTreeIds)
  return treeItemList.flat()
}

// getApplyContentsArrayByTree(['01', '02', '03', '04'])
